package Android.Billarm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.util.Log;

public class IconScreen extends Activity
{
    private static final String TAG = "IconScreen";
    public Bill bill;
    private IconAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate (Bundle state)
    {
        super.onCreate (state);

        // if (state != null) ; //TODO

        bill = (Bill) getIntent().getSerializableExtra("bill");

        setContentView (R.layout.icon_grid);
        adapter = new IconAdapter (this);
        ((GridView) findViewById (R.id.icon_grid))
            .setAdapter (adapter);

        ViewGroup group = (ViewGroup) findViewById (R.id.IconScreenPlainIcons);
        for (int color = 0; color < BillIcon.ncolors(); ++color)
        {
            ImageView im = new ImageView (this);
            BillIcon.setPlainColor (color, im);
            im.setOnClickListener (colorButtonListener);
            im.setId (color);
            group.addView (im);
        }

        for (int key : bill.icons)  addIconView (key);
    }

    public void doneClicked (View v)
    {
        Intent res = new Intent ();
        if (v.getId() == R.id.commit)
        {
            setResult (RESULT_OK, res);
            res.putExtra ("bill", bill);
        }
        else  setResult (RESULT_CANCELED, res);

        finish ();
    }

    /* Change the color of the icons */
    OnClickListener colorButtonListener = new OnClickListener ()
    {
        public void onClick (View v)
        {
            adapter.color = v.getId (); /* This is a BillIcon.<color> */
            adapter.notifyDataSetChanged ();
        }
    };

    private void addIconView (int key)
    {
        ImageView im = new ImageView (this);
        ViewGroup group = (ViewGroup)
            findViewById (R.id.IconScreenPicked);
        im.setOnClickListener (adapter.pickedPressed);
        BillIcon.setImageView (key, im);

        /* Add icon to screen and Bill object */
        group.addView (im);
    }

    /* This cycles thru the icons */
    private static class IconAdapter extends android.widget.BaseAdapter
    {
        public IconScreen screen;
        public int color;
        public IconAdapter (IconScreen c) { color = BillIcon.GREY; screen = c; }
        public int getCount() { return BillIcon.nicons(); }
        public Object getItem (int pos) { return pos; }
        public long getItemId (int pos) { return pos; }

        public void setColor (int c)
        {
            color = c;
            notifyDataSetChanged ();
        }

        public View getView (int pos, View v, ViewGroup parent)
        {
            ImageView im;
            if (v == null)
            {
                im = new ImageView (screen);
                /* im.setScaleType (ImageView.ScaleType.FIT_CENTER); */
                im.setOnClickListener (iconPressed);
            }
            else
            {
                im = (ImageView) v;
            }

            BillIcon.setImageView (pos, color, im);
            im.setId (pos);
            return im;
        }

        OnClickListener iconPressed = new OnClickListener ()
        {
            public void onClick (View v)
            {
                Bill bill = screen.bill;
                /* Don't have more than 4 */
                if (bill.icons.size() >= 4)  return;

                int key = BillIcon.iconKey (v.getId(), color);
                screen.addIconView (key);
                bill.icons.add (key);
            }
        };

        OnClickListener pickedPressed = new OnClickListener ()
        {
            public void onClick (View v)
            {
                LinearLayout group = (LinearLayout)
                    screen.findViewById (R.id.IconScreenPicked);

                int index = group.indexOfChild (v);
                /* Remove icon from screen and Bill object */
                group.removeView (v);
                screen.bill.icons.remove (index);
            }
        };
    }
}

